package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "c端购物车相关接口")
@Slf4j
public class ShoppingCartController {
  @Autowired
  private ShoppingCartService shoppingCartService;
  
  /**
   * 添加到购物车
   * @return
   */
  @PostMapping("/add")
  public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
    log.info("添加购物车：{}", shoppingCartDTO);
    shoppingCartService.add(shoppingCartDTO);
    return Result.success();
  }
  
  /**
   * 查询购物车
   * @return
   */
  @GetMapping("/list")
  public Result<List<ShoppingCart>> list() {
    List<ShoppingCart> carts = shoppingCartService.list();
    return Result.success(carts);
  }
  
  /**
   * 从购物车中减少或移除商品。
   * @return
   */
  @PostMapping("/sub")
  public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
    shoppingCartService.sub(shoppingCartDTO);
    return Result.success();
  }
  
  /**
   * 清空当前用户的购物车。
   * @return
   */
  @DeleteMapping("/clean")
  public Result clean() {
    shoppingCartService.clean();
    return Result.success();
  }
}
